﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SELF
{
    public partial class MessageBoxCustom : DevExpress.XtraEditors.XtraForm
    {
        #region constructor
        public MessageBoxCustom()
        {
            InitializeComponent();
            //this.Width = 560;
        }
        public MessageBoxCustom(CustomButton ButtonType)
        {
            InitializeComponent();
            //this.Width = 560;
        }
        public MessageBoxCustom(string textInput,int life)
        {
            InitializeComponent();
            DisplayText = textInput;
            BoxLife = life;
            //this.Width = 560;
        }
        #endregion
        private string displayText = "";
        public string DisplayText
        {
            set { displayText = value; }
        }
        private static DialogResult clickResult;
        public static DialogResult ResultDialog
        {
            get { return clickResult; }
            set { clickResult = value; }
        }

        private int boxLife = 0;
        private int currBoxLife = 0;

        private int CurrBoxLife
        {
            get { return currBoxLife; }
            set { currBoxLife = value; }
        }

        public int BoxLife
        {
            get { return boxLife; }
            set { boxLife = value; }
        }
        Timer tLife = new Timer();
        private void setBoxLife()
        {
            tLife.Interval = 1000;
            tLife.Start();
            tLife.Tick += new EventHandler(tLife_Tick);
        }

        void tLife_Tick(object sender, EventArgs e)
        {
            if (BoxLife == 0)
            {
                tLife.Stop();
            }
            CurrBoxLife += 1;
            if (CurrBoxLife == BoxLife)
            {
                tLife.Stop();
                this.Close();
                this.Dispose();
            }
            //throw new NotImplementedException();
        }

        private void frm_MessageBox_Load(object sender, EventArgs e)
        {
            setBoxLife();
            string textMessage = displayText;
            using (Graphics g = CreateGraphics())
            {
                SizeF size = g.MeasureString(textMessage, labelControl1.Font, 495);
                Console.WriteLine(size.Height);
                //labelControl1.Height = (int)Math.Ceiling(size.Height);
                //labelControl1.Text = textMessage;
            }

            int buttonX = (int)(Math.Round((double)this.Width / 2.0)) - 63;
            btPicOK.Location = new Point(buttonX, 0);
            labelControl1.Text = cutText(textMessage, 42);
        }

        private string cutText(string textLong,int length)
        {
            string newStr = "";
            string[] txtArray = textLong.Split(' ');
            int currentLen = 0;
            foreach (string word in txtArray)
            {
                currentLen += word.Length+1;
                if (currentLen > length)
                {
                    newStr += "\n"+word + " ";
                    currentLen = word.Length+1;
                }else
                    newStr += word+" ";
            }
            return newStr;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ResultDialog = DialogResult.Abort;
            this.DialogResult = DialogResult.Abort;
            this.Close();
            this.Dispose();
        }

        private void btPicOK_Click(object sender, EventArgs e)
        {
            ResultDialog = DialogResult.OK;
            this.DialogResult = DialogResult.OK;
            this.Close();
            this.Dispose();
        }

        private void drawButton()
        {

        }

        public enum CustomButton
        {
            OK
            ,OKCancel
            ,RetryCancel
            ,Confirm
            ,YesNo
            ,YesNoCancel
            ,AbortRetryIgnore
        }
    }
}